package com.tjec.project.mobile.controller;

import com.tjec.common.utils.SecurityUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.framework.web.page.TableDataInfo;
import com.tjec.project.businessWorkbench.domain.BmsBusiness;
import com.tjec.project.businessWorkbench.domain.BmsProjectDoc;
import com.tjec.project.mobile.service.MobileService;
import com.tjec.project.projectWorkbench.domain.BmsProject;
import com.tjec.project.projectWorkbench.domain.BmsProjectContract;
import com.tjec.project.projectWorkbench.domain.BmsProjectMember;
import com.tjec.project.projectWorkbench.domain.BmsProjectStakeholder;
import com.tjec.project.projectWorkbench.service.IBmsProjectContractService;
import com.tjec.project.projectWorkbench.service.IBmsProjectMemberService;
import com.tjec.project.projectWorkbench.service.IBmsProjectService;
import com.tjec.project.system.service.IBmsMdBusinessCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 移动端项目主页controller类
 *
 * @author ：cjj
 * @date ：Created in 2020/8/19 18:56
 */
@Api(value = "移动端项目主页接口" , tags = {"移动端项目主页接口"})
@RestController
@RequestMapping("/mobile/project")
public class ProjectWorkbenchController extends BaseController{

    @Autowired
    private MobileService mobileService;

    @Autowired
    private IBmsProjectMemberService bmsProjectMemberService;

    @Autowired
    private IBmsMdBusinessCategoryService bmsMdBusinessCategoryService;

    @Autowired
    private IBmsProjectContractService bmsProjectContractService;

    @Autowired
    private IBmsProjectService bmsProjectService;

    /**
     * 项目概况
     */
    @ApiOperation("项目概况")
    @GetMapping("/{projectId}")
    public AjaxResult general(@PathVariable("projectId") String projectId){
        BmsProject bmsProject = mobileService.selectBmsProjectByGuidMobile(projectId);
        return AjaxResult.success(bmsProject);
    }

    /**
     * 项目合同
     */
    @ApiOperation("查询项目合同")
    @GetMapping("/contractList")
    public TableDataInfo general(BmsProjectContract bmsProjectContract){
        BmsProject bmsProject = bmsProjectService.selectBmsProjectByGuid(bmsProjectContract.getProjectId());
        if(bmsProject==null){
            AjaxResult.error();
        }
        bmsProjectContract.setProjectId(bmsProject.getCode());
        startPage();
        List<BmsProjectContract> list = bmsProjectContractService.selectBmsProjectContractList(bmsProjectContract);
        return getDataTable(list);
    }

    /**
     * 项目成员列表
     */
    @ApiOperation("项目成员列表")
    @GetMapping("/memberList")
    public TableDataInfo member(BmsProjectMember bmsProjectMember){
        startPage();
        List<BmsProjectMember> list = mobileService.selectBmsProjectMemberMobileList(bmsProjectMember);
        /*for (BmsProjectMember projectMember : list) {
            projectMember.setProjectName(String.valueOf(bmsProjectMemberService.selectBmsProjectMemberByUserId(projectMember.getUserId())));
        }*/
        return getDataTable(list);
    }

    /**
     * 查询项目干系人列表
     */
    @ApiOperation("项目干系人列表")
    @GetMapping("/stakeholderList")
    public TableDataInfo stakeholder(BmsProjectStakeholder bmsProjectStakeholder)
    {
        startPage();
        List<BmsProjectStakeholder> list = mobileService.selectBmsProjectStakeholderMobileList(bmsProjectStakeholder);
        return getDataTable(list);
    }

    /**
     * 查询业务管理列表
     */
    @ApiOperation("业务管理列表")
    @GetMapping("/businessManagementList")
    public TableDataInfo businessManagement(BmsBusiness bmsBusiness)
    {
        Set<String> engineeringBiddingList = bmsMdBusinessCategoryService.selectEngineeringBidding();
        startPage();
        List<BmsBusiness> list = mobileService.selectBmsBusinessMobileList(bmsBusiness);
        for (BmsBusiness business : list) {
            if(bmsProjectMemberService.isTenderMembers(SecurityUtils.getLoginUser().getUser().getUserId(), business)
                    &&engineeringBiddingList.contains(business.getBusinessCategoryId())){
                business.setTenderMemberFlag("0");
            }else{
                business.setTenderMemberFlag("1");
            }
        }
        return getDataTable(list);
    }

    /**
     * 项目文档
     */
    @ApiOperation("项目文档")
    @GetMapping("/fileQueryTree")
    public AjaxResult fileQueryTree(BmsProjectDoc bmsProjectDoc)
    {
        Map<String,Object> trees = mobileService.lazyBuildFileQueryTreeMobile(bmsProjectDoc);
        return AjaxResult.success(trees);
    }
}
